<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="book" class="com.jsp.study.db.Book"></jsp:useBean>
<jsp:setProperty property="*" name="book"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图书添加</title>
</head>
<body>
	<%
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://118.89.160.58:3306/xbin_db?relaxAutoCommit=true&useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false";
			conn = DriverManager.getConnection(url, "xbin", "xbin0629");
			String sql = "insert into tb_books (name, price, bookcount, author) values (?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, book.getName());
			ps.setDouble(2, book.getPrice());
			ps.setInt(3, book.getBookCount());
			ps.setString(4, book.getAuthor());
			int row = ps.executeUpdate();
			out.println("图书名称：" +  book.getName() + ",价格：" + book.getPrice() + ",数量：" + book.getBookCount() + ",作者：" + book.getAuthor() + "\n");
			if (row > 0) {
				out.println("成功添加了" + row + "条记录");
			} else {
				out.println("添加记录失败");
			}
		} catch (Exception e) {
			out.println("操作数据库过程中出现异常");
		} finally {
			if (conn != null) conn.close();
			if (ps != null) ps.close();
		}
	%>
</body>
</html>